import Cookies from 'js-cookie'

const state = {
    sidebar: {
        opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
        withoutAnimation: false,
        hidden: Cookies.get('hideSidebar') ? !!+Cookies.get('hideSidebar') : true
    },
    channels: sessionStorage.getItem('channels') ? JSON.parse(sessionStorage.getItem('channels')) : [],
    device: 'desktop'
}

const mutations = {
    TOGGLE_SIDEBAR: state => {
        state.sidebar.opened = !state.sidebar.opened
        state.sidebar.withoutAnimation = false
        if (state.sidebar.opened) {
            Cookies.set('sidebarStatus', 1)
        } else {
            Cookies.set('sidebarStatus', 0)
        }
    },
    CLOSE_SIDEBAR: (state, withoutAnimation) => {
        Cookies.set('sidebarStatus', 0)
        state.sidebar.opened = false
        state.sidebar.withoutAnimation = withoutAnimation
    },
    TOGGLE_DEVICE: (state, device) => {
        state.device = device
    },
    TOGGLE_HIDDEN: (state, isHidden) => {
        state.sidebar.hidden = isHidden
        if (isHidden) {
            Cookies.set('hideSidebar', 1)
        } else {
            Cookies.set('hideSidebar', 0)
        }
    },
    SET_CHANNELS: (state, channels) => {
        state.channels = channels
        sessionStorage.setItem('channels', JSON.stringify(channels))
    }
}

const actions = {
    toggleSideBar({ commit }) {
        commit('TOGGLE_SIDEBAR')
    },
    closeSideBar({ commit }, { withoutAnimation }) {
        commit('CLOSE_SIDEBAR', withoutAnimation)
    },
    toggleDevice({ commit }, device) {
        commit('TOGGLE_DEVICE', device)
    },
    toggleHidden({ commit }, isHidden) {
        commit('TOGGLE_HIDDEN', isHidden)
    },
    setChannels({ commit }, channels) {
        commit('SET_CHANNELS', channels)
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}